package com.qihoo.finance.lcs.enhance.threadpool.impl;

import com.qihoo.finance.lcs.enhance.threadpool.*;
import com.qihoo.finance.msf.threadpool.ThreadPool;

import java.util.concurrent.Future;

public class ThreadPoolServiceImpl implements ThreadPoolService {

    private ThreadPool threadPool;

    public ThreadPoolServiceImpl(ThreadPool threadPool) {
        this.threadPool = threadPool;
    }

    @Override
    public void submit(WorkUnit workUnit, String workSeqNo, ThreadPoolNameService threadPoolNameService) {
        WorkContext taskContext = new WorkContext(workUnit, workSeqNo);
        threadPool.submit(taskContext, threadPoolNameService.getThreadPoolName());
    }

    @Override
    public void submit(WorkUnit workUnit, int workId, ThreadPoolNameService threadPoolNameService) {
        submit(workUnit, String.valueOf(workId), threadPoolNameService);
    }

    @Override
    public void submit(WorkUnit workUnit, long workId, ThreadPoolNameService threadPoolNameService) {
        submit(workUnit, String.valueOf(workId), threadPoolNameService);
    }

    @Override
    public <T> Future<T> call(WorkUnitCallable<T> workUnitCallable, String workSeqNo, ThreadPoolNameService threadPoolNameService) {
        WorkContextCallable<T> workContextCallable = new WorkContextCallable<T>(workUnitCallable, workSeqNo);
        return threadPool.submit(workContextCallable, threadPoolNameService.getThreadPoolName());
    }

    @Override
    public <T> Future<T> call(WorkUnitCallable<T> workUnitCallable, int workSeqNo, ThreadPoolNameService threadPoolNameService) {
        return call(workUnitCallable, String.valueOf(workSeqNo), threadPoolNameService);
    }

    @Override
    public <T> Future<T> call(WorkUnitCallable<T> workUnitCallable, long workSeqNo, ThreadPoolNameService threadPoolNameService) {
        return call(workUnitCallable, String.valueOf(workSeqNo), threadPoolNameService);
    }

    @Override
    public boolean isExists(ThreadPoolNameService threadPoolNameService) {
        return threadPool.isExists(threadPoolNameService.getThreadPoolName());
    }

    public ThreadPool getThreadPool() {
        return threadPool;
    }

    public ThreadPoolServiceImpl setThreadPool(ThreadPool threadPool) {
        this.threadPool = threadPool;
        return this;
    }
}
